<template>
	<view class="coupon-charge">
		<view class="coupon-charge-title">卡券券码</view>
		<view class="coupon-charge-input-box">
			<van-icon class="scan-icon" name="scan" @click="openCamera" />
			<input class="coupon-input" v-model="couponPsw" type="number" placeholder="请输入卡券券码"
				placeholder-class="placeholderClass">
		</view>
		<view class="charge-btn" @click.stop="chargeCoupon">确认核销</view>


	</view>
</template>

<script>
	import apiRqs from "@/api/api-request.js";
	import getDateTime from '@/common/getDateTime.js';
	export default {
		data() {
			return {
				couponPsw: "",
				use_time: "",
			}
		},
		onShow() {
			const couponPsw = uni.getStorageSync("couponPsw");
			if (couponPsw) {
				this.couponPsw = couponPsw;
				uni.removeStorageSync("couponPsw");
			}
		},
		onLoad() {
			this.use_time = getDateTime.dateTimeStr("y-m-dTh:i:s+08:00");
		},
		methods: {
			// 打开相机扫描
			openCamera() {
				uni.navigateTo({
					url: "/packageD/coupon/scan-camera"
				})
			},
			chargeCoupon() {
				const SubMchId = uni.getStorageSync("couponTokenInfo").SubMchId;
				const openid = uni.getStorageSync("couponTokenInfo").OpenId;
				let Randnum = '';
				for (var i = 0; i < 10; i++) {
					Randnum += Math.ceil(Math.random() * 10);
				};
				let p = {
					"coupon_code": this.couponPsw,
					"appid": "wxadc26d832d9c62d2",
					"use_time": this.use_time,
					"use_request_no": SubMchId + Randnum
				};
				console.log('核销数据', p);
				if (this.couponPsw == "") {
					getApp().showToast("请输入核销券码");
					return false;
				}
				getApp().showLoading();
				apiRqs._chargeCoupon(p).then(res => {
					uni.hideLoading();
					console.log('核销结果', res);
					if (res.code == 200 && res.data.resultCode.stateCode == "200") {
						getApp().showToast('核销成功', 'success');
						this.couponPsw = "";
					} else {
						const additional = JSON.parse(res.data.resultCode.additional);
						console.log("additional", additional);
						getApp().showToast(additional.message);
					}

				})
			},

		},
	}
</script>

<style lang="less">
	page {
		background-color: #f5f5f5;
	}

	.coupon-charge {
		.coupon-charge-title {
			margin: 20px;
			font-size: 12px;
			color: rgba(0, 0, 0, 0.40);
		}

		.coupon-charge-input-box {
			background-color: #fff;
			border-radius: 4px;
			margin: 10px 20px 0 20px;
			display: flex;
			align-items: center;
			padding: 10px 15px;

			.scan-icon {
				width: 20px;
				flex: 0 0 20px;
				font-size: 20px;
				color: rgba(0, 0, 0, 0.30);
			}

			.coupon-input {
				flex: 1;
				min-width: 0;
				border: none;
				font-size: 14px;
				color: #666;
				margin-left: 8px;
			}

			.placeholderClass {
				color: #ddd;
			}
		}

		.charge-btn {
			margin: 20px;
			border: none;
			background-color: #FFA53D;
			color: #fff;
			font-size: 15px;
			height: 35pt;
			line-height: 35pt;
			box-shadow: inset 0px 4px 7px 0px rgba(255, 255, 255, 0.33);
			border-radius: 4px;
			text-align: center;

			&::after {
				border: none;
			}

			&:active {
				background-color: rgba(255, 141, 10, 1);
			}
		}
	}
</style>